@extends('admin')

@section('content')

    <p class="mb10">
        <a href="{{url('admin/product/update')}}" class="btn J_dialog" title="发布产品"><span class="add"></span>发布产品</a>
    </p>

    <div class="h_a">搜索</div>
    <div class="search_type cc mb10">
        <form action="{{url('admin/product')}}" method="get" class="search_form">
            <label for="search_title">产品名称：</label>
            <input type="text" name="name" value="{{isset($appends['name']) ? $appends['name'] : ''}}" id="search_title"
                   class="input length_2 mr10"/>
            <label for="search_title">产品类别：</label>
            <select id="parent_id" name="parent_id">
                <option value="0" {{!isset($appends['parent_id']) || 0 == $appends['parent_id'] ? 'selected="selected"' : ''}}>
                    所有类别
                </option>
                @foreach($parents as $key=>$parent)
                    <option value="{{$key}}" {{isset($appends['parent_id']) && $key == $appends['parent_id'] ? 'selected="selected"' : ''}}>{{$parent}}</option>
                @endforeach
            </select>
            <select id="type_id" name="type_id" data-type_id="{{isset($appends['type_id']) ? $appends['type_id'] : ''}}"
                    disabled style="display: none">

            </select>
            <button type="submit" class="btn">提交查询</button>
        </form>

    </div>

    <div class="table_list">
        <div id="data_list">
            <table class="items">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>产品名称</th>
                    <th>所属类别</th>
                    <th>发布者</th>
                    <th>发布时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($list as $item)
                    <tr>
                        <td>{{$item->id}}</td>
                        <td>{{$item->name}}</td>
                        <td>{{  isset( $item->type ) ? $item->type->name : '' }}</td>
                        <td>{{$item->poster->nick}}</td>
                        <td>{{$item->created_at}}</td>
                        <td>
                            <a href="{{url('admin/product/update/'.$item->id)}}" title="编辑 {{$item->name}}"
                               class="mr10 J_dialog">[编辑]</a>
                            <a href="{{url('admin/product/delete/'.$item->id)}}" class="mr10 J_ajax_del">[删除]</a>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>

        <div class="summary">
            共有 {{$list->total()}} 条数据
        </div>
        @if ($list->hasPages())
            <div class="pager">
                {!! $list->appends($appends)->render() !!}
            </div>
        @endif
    </div>
@stop
@section('js')
    @parent
    <script>
        $(function () {
            load();

            $('#parent_id').on('change', function () {
                __url = "/admin/product/type/" + $(this).val();
                $.post(__url, {_token: '{{ csrf_token() }}'}, function (datas) {
                    $('#type_id').html('');
                    $('#type_id').css('display', '');
                    $('#type_id').attr('disabled', false);
                    for (var data in datas.data) {
                        $('#type_id').append(' <option value="' + data + '">' + datas.data[data] + '</option>')
                    }
                    console.log(data);
                })
            })
        });

        function load() {
            if (0 != $('#type_id').val($('#type_id').data('type_id'))) {
                __url = "/admin/product/type/" + $('#parent_id').val();
                $.post(__url, {_token: '{{ csrf_token() }}'}, function (datas) {
                    $('#type_id').html('');
                    $('#type_id').css('display', '');
                    $('#type_id').attr('disabled', false);
                    for (var data in datas.data) {
                        $('#type_id').append(' <option value="' + data + '">' + datas.data[data] + '</option>')
                    }
                    $('#type_id').val($('#type_id').data('type_id'));
                })
            }
        }
    </script>
@stop